// Copyright (c) 1994-1996 Apple Computer, Inc.  All rights reserved.


// Transport service type constants

constant kTCP :=	1;
constant kUDP :=	2;


// Utility functions for Internet applications

DefConst(	'kNumToHostAddr,
				func(address)				// Convert a 4 byte array to a "w.x.y.z" string
				begin
					if IsArray(address)
					and Length(address) = 4 then
						NumberStr(address[0]) & "." & NumberStr(address[1]) & "." & NumberStr(address[2]) & "." & NumberStr(address[3])
					else
						"";
				end	);


DefConst(	'kHostAddrToNum,
				func(addressStr)			// Convert a "w.x.y.z" string to a 4 byte address array
				begin
					local result := [0, 0, 0, 0];
					if IsString(addressStr) then
						begin
							local token, closure := StrTokenize(TrimString(Clone(addressStr)), $.);
							for i:=0 to 3 do
								if token := call closure with () then
									if token := StringToNumber(token) then
										result[i] := Min(Max(RintToL(NearbyInt(token)), 0), 255);
						end;
					result;
				end	);


DefConst(	'kIsIPAddr,
				func(addressStr)			// Return True if addressStr is an IP address string of the form "w.x.y.z" (as opposed to a domain name string)
				begin
					IsString(addressStr) and																	// return True if addressStr is a string
					StrLen(StringFilter(addressStr, "0123456789.", 'rejectAll)) = 0		// and contains only chars 0-9 and period
					and StrReplace(Clone(addressStr), ".", ".", nil) = 3							// and contains three periods
					and StrExactCompare(	addressStr,													// and converting it to and from array format gives the same address string
														call kNumToHostAddr with (call kHostAddrToNum with (addressStr))) = 0;
				end	);


DefConst(	'kGetEndpointConfigOptions,
				func(linkID, protocol)		// protocol:  1 = TCP, 2 = UDP
				begin
					[
						{	label:		"inet",
							type:		'service,
							opCode:		opSetRequired,
							result:		nil,	},
						
						{	label:		"ilid",			// set the link id
							type:		'option,
							opCode:		opSetRequired,
							result:		nil,
							form:		'template,
							data:		{
								arglist:		[
									linkID,	],
								typelist:	[	'struct,
									'ulong,	],	},	},
						
						{	label:		"itsv",			// set the transport protocol (TCP or UDP)
							type:		'option,
							opCode:		opSetRequired,
							result:		nil,
							form:		'template,
							data:		{
								arglist:		[
									protocol,	],
								typelist:	[	'struct,
									'ulong,	],	},	},
					];
				end	);


DefConst(	'kUDPReceiveOptions,
				[
					{	label:		"iuds",				// get the UDP destination socket
						type:		'option,
						opCode:		opGetCurrent,
						result:		nil,
						form:		'template,
						data:		{
							arglist:		[
								[0, 0, 0, 0],				// IP address
								0,		],						// destination port number	
							typelist:	[	'struct,
								['array, 'byte, 4],
								'short,	],	},	},
					
					{	label:		"iuss",				// get the UDP source socket
						type:		'option,
						opCode:		opGetCurrent,
						result:		nil,
						form:		'template,
						data:		{
							arglist:		[
								[0, 0, 0, 0],				// IP address
								0,		],						// destination port number	
							typelist:	[	'struct,
								['array, 'byte, 4],
								'short,	],	},	},
				]	);


DefConst(	'kINetBindOptions,
				func(localPort, useDefaultPort)
				[
					{	label:		"ilpt",				// set the local port
						type:		'option,
						opCode:		opSetRequired,
						result:		nil,
						form:		'template,
						data:		{
							arglist:		[
								localPort,					// local port number
								useDefaultPort,	],		// use default port	
							typelist:	[	'struct,
								'short,
								'boolean,	],	},	},
				]	);


DefConst(	'kTCPConnectOptions,
				func(remoteAddr, remotePort)
				[
					{	label:		"itrs",				// set the TCP remote socket
						type:		'option,
						opCode:		opSetRequired,
						result:		nil,
						form:		'template,
						data:
						{
							arglist:		[
								remoteAddr,
								remotePort,	],			// remote port number	
							typelist:	[
								'struct,
								['array, 'byte, 4 ],
								'short,	],
						},
					},
				]	);


DefConst(	'kTCPListenOptions,
				[
					{	label:		"itrs",				// get the TCP remote socket
						type:		'option,
						opCode:		opGetCurrent,
						result:		nil,
						form:		'template,
						data:		{
							arglist:		[
								[0, 0, 0, 0],				// IP address
								0,		],						// destination port number	
							typelist:	[	'struct,
								['array, 'byte, 4],
								'short,	],	},	},
				]	);


DefConst(	'kGetIPAddressesOptions,
				[
					{	label:		"iprf",
						type:		'option,
						opCode:		opGetCurrent,
						result:		nil,
						form:		'template,
						data:		{
							arglist:		[
								[0, 0, 0, 0],
								[0, 0, 0, 0],	],
							typelist:	[	'struct,
								['array, 'byte, 4],
								['array, 'byte, 4],	],	},	}
				]	);


DefConst(	'kUDPPutBytesOptions,
				func(addr, port)
				[
					{	label:		"iuds",				// set the UDP destination socket
						type:		'option,
						opCode:		opSetRequired,
						result:		nil,
						form:		'template,
						data:		{
							arglist:		[
								addr,
								port,	],						// remote port number	
							typelist:	[	'struct,
								['array, 'byte, 4],
								'short,	],	},	}
				]	);		


